package com.shu.project.controller;

import com.shu.project.model.User;
import com.shu.framework.redis.UserKey;
import com.shu.framework.result.Result;
import com.shu.project.service.IUserService;
import com.shu.framework.redis.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.shu.framework.result.Result.success;

@Controller
public class SampleController {
    @Autowired
    private IUserService userService;
    @Autowired
    RedisService redisService;
    @RequestMapping("/view")
    public String view(Model model){
        model.addAttribute("name","shujinhao");
        return "login";
    }
    @RequestMapping("/db/get")
    @ResponseBody
    public Result<User> queryUser(Model model){
        User user = userService.queryUserById(1l);
        /*ValueOperations<String, String> stringStringValueOperations = template.opsForValue();
        stringStringValueOperations.set("shu",JSONObject.toJSONString(user));*/
//        redisService.set(UserKey.getById,"1",user);
        redisService.set(UserKey.getByName,user.getNickName(),user);
        return success(user);
    }
    @RequestMapping("/queryRedis")
    @ResponseBody
    public Result<User> queryRedis(){
//        User user = redisService.get(UserKey.getById,"1", User.class);
        User user =redisService.get(UserKey.getByName,"shujinhao",User.class);
        return Result.success(user);
    }
    @RequestMapping("/setKey1")
    @ResponseBody
    public Result<Long> setKey1(){
        Long longs =redisService.incr(UserKey.getNum,"2");
        return success(longs);
    }
}
